package com.fu.edu.manager.dao;

import com.fu.edu.manager.domain.Student;
import com.fu.edu.manager.domain.Teacher;

/**
 * @author 付大大
 * @create 2021-01-06 17:24
 */
public class TeacherDao {
    private static Teacher[] teachers = new Teacher[5];

    static {
        Teacher teacher1 = new Teacher("2006910242", "付大大", "23", "20170101");
        Teacher teacher2 = new Teacher("2006910241", "周大大", "23", "20170101");
        teachers[0] = teacher1;
        teachers[1] = teacher2;
    }


    public boolean addTeacher(Teacher teacher) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher1 = teachers[i];
            if (teacher1 == null) {
                index = i;
                break;
            }
        }

        if (index != -1) {
            teachers[index] = teacher;
            return true;
        } else {
            return false;
        }
    }


    public Teacher[] findAllTeacher() {
        return teachers;
    }

    public void deleteTeacherById(String id) {
        int index = getIndex(id);
        teachers[index] = null;
    }


    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null && teacher.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateTeacher(String id, Teacher teacher) {
        int index = getIndex(id);
        teachers[index] = teacher;
    }
}
